#include <gtk/gtk.h>
#include <string.h>
#include <stdlib.h>
#include "rw_config.h"
#include "stroke.h"

static void add_point (Stroke *stk, Point p)
{
	gchar *buf, *s;	
	
	buf = g_strdup_printf ("%d,%d,0", p.x, p.y);	
	
	s = stk->cstroke;
	s+=stk->ncstroke;
	stk->ncstroke+=strlen(buf);		
	strcpy (s,buf);		
	g_free (buf);
	
	stk->stroke[stk->i_point].x = stk->last.x = p.x;
	stk->stroke[stk->i_point].y = stk->last.y = p.y;
	stk->pPoints[stk->i_point*2] = p.x ;
  	stk->pPoints[stk->i_point*2+1] = p.y ;
	stk->i_point++;
}

void add_point_start(Stroke *stk,Point p)
{
	gchar *buf,*s;

	buf=g_strdup_printf("%d,%d,0",p.x,p.y);

	s=stk->cstroke;
	s+=stk->ncstroke;
	stk->ncstroke+=strlen(buf);
	strcpy(s,buf);
	g_free(buf);
	
	stk->stroke[stk->i_point].x=stk->last.x=p.x;
	stk->stroke[stk->i_point].y=stk->last.y=p.y;

}



Stroke * stroke_create (int w,int h)
{
	Stroke * stk;
    gchar modefilepath[256];
	stk = g_new0 (Stroke, 1);
    
		/*
	
    stk->surface = cairo_image_surface_create (CAIRO_FORMAT_A1, w, h);
    
    */
      
    

	stk->i_point = 0;
	stk->last.x = -1;
	stk->last.y = -1;
	
	stk->ncstroke = 0;
	memset (stk->cstroke, 0, STROKE_MAX_POINT*4);
	stk->status = STROKE_READY;
	
	/* 初始化手写引擎 */

    
	stk->recognizer=shuxie_recognizer_new();	    
    sprintf(modefilepath,"%s/handwrite.model",getUserModelDir());
    shuxie_recognizer_loadModel(stk->recognizer,modefilepath,1);
    
    DIR* pDir;  
    struct dirent* ptr;  
    if( !(pDir = opendir(SHUXIE_RECOGNIZER_SYS)) )  
        goto charinit;  
    while( (ptr = readdir(pDir)) != 0 )  
    {  
        /*装入SHUXIE_RECOGNIZER_SYS目录下的model文件*/  
        //printf("%s\n",ptr->d_name);
        if(strcmp(ptr->d_name,".model")>0){
            sprintf(modefilepath,"%s/%s",SHUXIE_RECOGNIZER_SYS,ptr->d_name);
        if(!shuxie_recognizer_loadModel(stk->recognizer,modefilepath,0))
        {
            fprintf(stderr ,"ERROR:%s\n","字库装入出错");        
        }
        }
    }  
    closedir(pDir);   
    
    
    
    charinit:
    if(w<=0)
        w=300;
    if(h<=0)
        h=300;
    if(w>1000)
        w=1000;
    if(h>1000)
        h=1000;
    stk->width=w;
    stk->height=h;
    
    stk->charactera=shuxie_character_new();
    shuxie_character_clear(stk->charactera);
    shuxie_character_initSize(stk->charactera,w,h);
    
   	stk->newcharactera=shuxie_character_new();   
    shuxie_character_clear(stk->newcharactera);
    shuxie_character_initSize(stk->newcharactera,w,h);
    
	return stk;
}
/*****************************
*
* 开始
*
*****************************/
int stroke_start (Stroke *stk)
{
	if (stk->status != STROKE_READY)		
		return -1;
	stk->status = STROKE_START;	
	return 0;
}

int stroke_stop( Stroke *stk)
{
	if(stk->status!=STROKE_START)		
		return -1;
	stk->status=STROKE_END;	
	return 0;
}

int stroke_clean( Stroke *stk)
{
    if(!stk)
        return -1;

	if(stk->status!=STROKE_END)	
		return -1;

	

	stk->i_point=0;
	stk->last.x=-1;
	stk->last.y=-1;

	stk->ncstroke=0;


	stk->status=STROKE_READY;	
	return 0;
}
/**********************************
* 存储point
*
**********************************/
int stroke_store (Stroke *stk, Point p)
{
	if( stk->status != STROKE_START) {
		return -1;
	}
	if (stk->i_point >= STROKE_MAX_POINT) {
		return -1;
	}
	if (p.x == -1 && p.y == -1) {
		//add_point(stk,p);		
		add_point_start(stk,p);
		return 0;		
	}
	
	if(stk->last.x == -1 && stk->last.y == -1) {
	/*	cairo_move_to (stk->cairo, p.x, p.y);		
		cairo_line_to (stk->cairo, p.x+1, p.y+1);	
		cairo_set_line_width (stk->cairo, 3.0);
		cairo_set_line_cap (stk->cairo, CAIRO_LINE_CAP_ROUND);	
		cairo_stroke  (stk->cairo);	
*/
		add_point (stk,p);		
		return 0;	
	}
	 


	if(abs(stk->last.x-p.x)<5&&abs(stk->last.y-p.y)<5)	
	{
		return 0;	
	}
/*
	cairo_move_to (stk->cairo, stk->last.x, stk->last.y);
	cairo_line_to (stk->cairo, p.x, p.y);
	cairo_set_line_width (stk->cairo, 3.0);
	cairo_set_line_cap (stk->cairo, CAIRO_LINE_CAP_ROUND);
	cairo_stroke  (stk->cairo);*/
	add_point(stk,p);		
	
	return 0;
}

Point * stroke_extract( Stroke *stk,int *count)
{
	if(stk->status!=STROKE_END)
		return NULL;
	*count=stk->i_point;
	return stk->stroke;
}

